<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html lang="zh">
<head>
<%@include file="/WEB-INF/jsp/common/header.jsp"%>
<link href="<%=request.getContextPath()%>/resources/form-select2/select2.css" type="text/css" rel="stylesheet">
<script src='${pageContext.request.contextPath}/resources/form-select2/select2.js'></script>
<script src='${pageContext.request.contextPath}/resources/form-select2/select2_locale_zh-CN.js'></script>
<style type="text/css">
.addForm {
	width: 100px;
}
</style>
<script type="text/javascript">
	var roomListTable;
	$(document).ready(function() {
		$("#resetForm").click(function() {
			$("#form input").val("");
		});
		$("#save").click(function() {
			var id = $(".modal-body input[name=id]").val();
			var name = $(".modal-body input[name=name]").val();
            var remark = $(".modal-body input[name=remark]").val();
            var hotelId = $("#hotelSearchInput").val();

			if ($.trim(name) === "") {
				$.messager.alert("房间名不能为空");
				return false;
			}
            if (hotelId === '') {
                $.messager.alert("房间类型必选");
                return false;
            }
			var url = contextpath + "/web/room/update";
            if(id === ''){
                url = contextpath + "/web/room/insert";
            }
			$(this).attr("disabled","disabled");
			$.ajax({
				type : "POST",
				url : url,
				data : $('#editForm').serialize(),
				success : function(result) {
                    $("#save").removeAttr("disabled");
                    if (result.code === 200) {
                        $("#editModal").modal("hide");
                        $.messager.alert("保存成功");
                        roomListTable.ajax.reload(null, false);
                    } else {
                        $.messager.alert(result.msg);
                    }
				}
			});
		});
        roomListTable = $('#roomListTable').DataTable({
            "bSort": true, //是否启动各个字段的排序功能
            "aaSorting": [[4, "desc"]], //默认的排序方式
            "aoColumns": [
                {
                    "mDataProp": "name",
                    "sTitle":"名称",
                    "sDefaultContent": "--",
                    "sWidth": "20%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : true,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "hotelName",
                    "sTitle":"房间类型",
                    "sDefaultContent": "--",
                    "sWidth": "15%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData !== '--') $(nTd).attr('title',sData);
                    }
                },
                {
                    "mDataProp": "monitorSn",
                    "sTitle":"监控设备号",
                    "sDefaultContent": "--",
                    "sWidth": "15%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData === '--') {
                            $(nTd).html('未绑定');
                        }
                    }
                },
                {
                    "mDataProp": "status",
                    "sTitle":"状态",
                    "sDefaultContent": "--",
                    "sWidth": "15%",
                    "sClass": "center flow",
                    "bSortable": false,//是否支持排序
                    "bSearchable" : false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if(sData === '--' || sData === 0) {
                            $(nTd).html('不能预约');
                        }else{
                            $(nTd).html('可预约');
                        }
                    }
                },
                {
                    "mDataProp": "createTime",
                    "sTitle": "创建时间",
                    "sWidth": "15%",
                    "sDefaultContent": "--",
                    "bSortable": true,
                    "sClass": "center flow",
                    "bSearchable": false,//是否支持条件查询
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        if (sData !== '--') $(nTd).attr('title', sData);
                    }
                },
                {
                    "mDataProp": "id",
                    "sTitle": "操作",
                    "sWidth": "120px",
                    "sClass": "center flow",
                    "bSearchable": false,
                    "bSortable": false,
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<a class='btn btn-xs btn-default' href='javascript:void(0);' onclick='editRoom(" + iRow + ")'><i class='fa fa-pencil'></i>&nbsp;编辑</a>&nbsp;&nbsp;")
                            .append("<a class='btn btn-xs btn-default' href='javascript:void(0);' onclick='deleteRoom(" + iRow + ")'><i class='fa fa-times'></i>&nbsp;删除</a>&nbsp;&nbsp;");
                    }
                }
            ],
            "ajax": {
                url: contextpath + '/web/room/queryPage',
                type:"post",
                data: function(data){
                    $.xDataTablesParams(data, $.trim($('#name').val()));
                    return data;
                }
            }
        });

        $('#editModal').on('hide.bs.modal', function () {
            $('#editForm :input').val('');
            $("#hotelSearchInput").select2('val', '');
            $("#save").removeAttr("disabled");
            $("#editForm .select2-chosen").text('房间类型筛选');
        });

        $("#hotelSearchInput").select2({
            placeholder: "房间类型筛选",
            minimumInputLength: 2,
            width: '200px',
            allowClear: true,
            ajax: {
                url: contextpath + "/web/hotel/queryPage",
                dataType: 'json',
                type:'post',
                quietMillis: 500,
                data: function (term, page) {
                    return {
                        'search.name': term, //search term
                        'start': (page-1)*10,
                        'length': 10
                    };
                },
                results: function (data, page) {
                    var more = page > 5? true : (page * 10) < data.recordsTotal;
                    return {results: data.data, more: more};
                }
            },
            formatResult: function(hotel){
                return hotel.name;
            },
            formatSelection: function(hotel){
                return hotel.name;
            },
            dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
            escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
        });
    });

    function editRoom(row){
        $('#modalTitle').text('编辑房间信息');
        var rowData = roomListTable.row(row).data();
        $('#editModal :input[name=id]').val(rowData.id);
        $('#editModal :input[name=name]').val(rowData.name);
        $('#editModal :input[name=monitorSn]').val(rowData.monitorSn);
        $('#editModal :input[name=remark]').val(rowData.remark);
        $('#editModal :input[name=status]').val(rowData.status);
        if(rowData.hotelName != null){
            $("#editForm .select2-chosen").text(rowData.hotelName);
        }
        if(rowData.hotelId != null){
            $("#hotelSearchInput").val(rowData.hotelId);
        }

        $('#editModal').modal('show');
    }

    function addRoom(){
        $('#modalTitle').text('新增房间信息');
        $("#editModal .select2-chosen").text('房间类型筛选');
        $('#editModal').modal('show');
    }

    function deleteRoom(row){
        var rowData = roomListTable.row(row).data();
        $.messager.confirm("确定", "真的要删除'" + rowData.name + "'吗？", function(){
            $.ajax({
               url : contextpath + '/web/room/delete/' + rowData.id,
               type : 'DELETE',
               success :  function(data){
                   if (data.code === 200) {
                       $.messager.alert("提示", "删除成功");
                       roomListTable.ajax.reload();
                   } else {
                       $.messager.alert(data.msg);
                   }
               }
            });
        });
    }

    function searchTable(){
        roomListTable.ajax.reload();
    }

</script>
</head>
<body>
	<div class="container-fluid">
		<div class="sub-header">
			<h2>房号列表</h2>
		</div>
		<br/>
		<form id="form" class="form-inline" role="form" action="#" method="post">
			<div class="form-group">
				<div class="input-group">
					<span class="input-group-addon">名称</span>
					<input type="text" class="form-control" id="name" placeholder="名称" style="width: 150px;">
				</div>
			</div>
			<button type="button" class="btn btn-default" onclick="searchTable()">查询</button>
			<button id="add" type="button" class="btn btn-info" onclick="addRoom()">新增房号</button>
		</form>
		<br>
		<div>
			<table class="table table-hover table-striped table-bordered" style="table-layout:fixed;" id="roomListTable"></table>
		</div>
	</div>

	<div class="modal fade" id="editModal" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-lg" style="width: 600px;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
					</button>
					<h4 class="modal-title" id="modalTitle">房间信息</h4>
				</div>
				<div class="modal-body">
                    <form id="editForm">
                        <input type="hidden" name="id">
                        <table id="editTable" class="table-hover table table-bordered">
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">房间号</span>
                                        <input type="text" class="form-control" name="name" placeholder="房间号" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">房间类型</span>
                                        <input type="hidden" name="hotelId" id="hotelSearchInput" style="width: 320px;"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon addForm">监控设备：</span>
                                        <input type="text" class="form-control" name="monitorSn" placeholder="监控设备号" style="width: 320px;">
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon">状态</span>
                                        <select name="status" class="form-control" style="width: 320px;">
                                            <option value="0">不能预约</option>
                                            <option value="1">可预约</option>
                                        </select>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="input-group">
                                        <span class="input-group-addon">备注</span>
                                        <textarea class="form-control" name="remark" rows="3" placeholder="备注"></textarea>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td align="center">
                                    <button id="save" type="button" class="btn btn-success addForm">保 存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    <button id="closeModal" type="button" onclick="$('#editModal').modal('hide');" class="btn btn-danger addForm">关闭</button>
                                </td>
                            </tr>
                        </table>
                    </form>
				</div>
			</div>
		</div>
	</div>


</body>